/**
 * @作者 Lucas
 * @创建时间 2020/5/17 19:54
 */
package com.lucas.springboot_restful_api.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lucas.springboot_restful_api.core.UnifyResponse;
import com.lucas.springboot_restful_api.dto.UserLoginDTO;
import com.lucas.springboot_restful_api.dto.UserRegisterDTO;
import com.lucas.springboot_restful_api.exception.http.ParameterException;
import com.lucas.springboot_restful_api.exception.http.ServerErrorException;
import com.lucas.springboot_restful_api.mapper.UserMapper;
import com.lucas.springboot_restful_api.model.User;
import com.lucas.springboot_restful_api.service.UserService;
import com.lucas.springboot_restful_api.util.CommonUtil;
import com.lucas.springboot_restful_api.util.JwtToken;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User>
        implements UserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public UnifyResponse loginByEmail(UserLoginDTO dto) throws Exception {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", dto.getAccount());
        User result = userMapper.selectOne(queryWrapper);

        if (ObjectUtils.isEmpty(result)) {
            throw new ParameterException(10007);
        }
        if (!CommonUtil.md5Verify(dto.getPassword(), result.getPassword())) {
            throw new ParameterException(10008);
        }
        String token = JwtToken.makeToken(result.getUnifyUid());
        Map<String, String> data = new HashMap<>();
        data.put("token",token);
        return new UnifyResponse("登录成功", data);
    }

    @Override
    public UnifyResponse registerByEmail(UserRegisterDTO dto) throws Exception {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("email", dto.getAccount());
        User result = userMapper.selectOne(queryWrapper);

        if (ObjectUtils.isNotEmpty(result)) {
            throw new ParameterException(10009);
        }

        String uuid = CommonUtil.makeUUID();
        User user = User.builder()
                .mobile(dto.getAccount())
                .unifyUid(uuid)
                .email(dto.getAccount())
                .password(CommonUtil.md5Encrypt(dto.getPassword1()))
                .nickname(dto.getNikeName())
                .build();

        int insert = userMapper.insert(user);

        if (ObjectUtils.isEmpty(insert)) {
            throw new ServerErrorException(1000);
        }

        String token = JwtToken.makeToken(uuid);

        return new UnifyResponse("注册成功",
                new HashMap<String, String>().put("token", token));
    }

    @Override
    public User selectUserByUnifyId(Long unifyUid) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("unify_uid", unifyUid);
        return userMapper.selectOne(queryWrapper);
    }
}
